How to take value from "PrimarySmtpAddress" and paste it into "WindowsEmailAddress" in Powershell?
I have mail-enabled contacts that are missing WindowsEmailAddress. I need to propogate PrimarySmtpAddress into WindowsEmailAddress field. I have 3 commands in mind and I'm trying to figure out how to pipe them right.Something like:Get-MailContact | FL PrimarySmtpAddress | Set-MailContact -WindowsEmailAddressBut it errors out with:Set-MailContact : Missing an argument for parameter 'WindowsEmailAddress'. Specify a parameter of type 'Microsoft.Exchange.Data.SmtpAddress' and try again.At line:1 char:78+ Get-MailContact | FL PrimarySmtpAddress | set-mailcontact -WindowsEmailAddress <<<<May be I need to put together some script and save it as .PS1 file?Any ideas?Thanks
August 12th, 2008 7:54pm
Forone Mail Contact...
Get-MailContact MailContactName | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress
Verify it...
Get-MailContact MailContactName| FLName, PrimarySmtpAddress, WindowsEmailAddress
For all MailContact in your environment
Get-MailContact | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress
Free Windows Admin Tool Kit Click here and download it now
August 12th, 2008 8:19pm
Thanks Amit. Did it actually work for you? Because I'm getting some error:[PS] C:\2>Get-MailContact "Tim Sage" | FL DisplayName,PrimarySmtpAddress,WindowsEmailAddressDisplayName : Tim SagePrimarySmtpAddress : aaa@aaa.aaaWindowsEmailAddress :[PS] C:\2>Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddressSet-MailContact : Cannot bind parameter 'WindowsEmailAddress' to the target. Exception setting "WindowsEmailAddress": "Object reference not set to an instance of an object."At line:1 char:66+ Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress <<<< $_.PrimarySmtpAddress
August 12th, 2008 9:42pm
I tried this as well and it did not work for me. You may actually not be able to accomplish this with a one-liner. If you set the value of Get-MailContact to a variable and then look at $VarName.PrimarySMTPAddress, you will see that the information is not actually presented as an SMTP. I think you can apply a method to it to set the output value to a string, but I could not figure it out. Of course, it is late there. :-(
Free Windows Admin Tool Kit Click here and download it now
August 13th, 2008 9:59am
No, I didnt try that since my test environment is being rebuilt. Here is the script you can try with if you are still looking for it...(again, I didnt try this but theoretical it should work)
========================================
$Contacts = Get-MailContact MailContactName
ForEach ($Contact in $Contacts)
{
$WEA = $Contact.PrimarySmtpAddress
$WEA = $WEA.ToString()
Set-Contact $Contact - WindowsEmailAddress $WEA
$WEA = $Null
}
========================================
You can run onall Contact by this
========================================
$Contacts = Get-MailContact
ForEach ($Contact in $Contacts)
{
$WEA = $Contact.PrimarySmtpAddress
$WEA = $WEA.ToString()
Set-Contact $Contact - WindowsEmailAddress $WEA
$WEA = $Null
}
========================================
August 14th, 2008 12:04pm
Thanks Amit. Did it actually work for you? Because I'm getting some error: [PS] C:\2> Get-MailContact "Tim Sage" | FL DisplayName,PrimarySmtpAddress,WindowsEmailAddress DisplayName : Tim Sage PrimarySmtpAddress : aaa@aaa.aaa WindowsEmailAddress : [PS] C:\2> Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress Set-MailContact : Cannot bind parameter 'WindowsEmailAddress' to the target. Exception setting "WindowsEmailAddress": "Object reference not set to an instance of an object." At line:1 char:66 + Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress <<<< $_.PrimarySmtpAddress
This is exactly 100% what is happening to me. Did you find a fix for this? The contacts show as having the correct (and only) primary smtp, but I get the "object reference not set to an instance of an object." when i actually run the code to set-mailcontact .... Any help would be appreciated.
Free Windows Admin Tool Kit Click here and download it now
April 9th, 2009 4:11am
What about using the loging account and not your display name ? "Tim Sage"
June 2nd, 2010 9:40am
I did this command and works form me
get-mailbox username |fl primarysmtpaddress
Greetings
Free Windows Admin Tool Kit Click here and download it now
March 9th, 2011 1:25am
Get-MailContact “MailContactName” | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress
Quote Evan Dodds MSFT
Will not work because in this syntax, the pipeline object variable ($_) is not available. Since you’re directly pipelining from Get | Set here, we bind the output object to instance parameter and stream the data into the Set. There is no explicit
iteration, and no script-block to host the $_ variable.
I don’t know that this syntax below does what you want, but at the very least it’ll activate the pipeline object variable by injecting a foreach-object explicit iteration into the operation (and also doing a per-iteration explicit binding of the
pipeline object to identity parameter since we’re losing the implicit pipeline binding we had in the earlier syntax):
Get-MailContact “MailContactName” | % { Set-MailContact $_-WindowsEmailAddress $_.PrimarySmtpAddress }
James Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com
March 9th, 2011 2:05pm